package models

import (
	"math"

	"github.com/astaxie/beego/orm"
)

//商品类型基础信息
type GoodsType struct {
	Id              int64
	Parent_id       int64
	Type_name       string
	Is_top          int
	Goods_brand_ids string
}

//商品类型SEO
type GoodsTypeSeo struct {
	Id          int64
	Type_id     int64
	Title       string
	Keyword     string
	Description string
}

func (gt *GoodsType) TableName() string {
	return "shop_goods_type"
}

func init() {
	orm.RegisterModel(new(GoodsType), new(GoodsTypeSeo))
}

//新增商品类型
func AddGoodsTypeData(parent_id int64, type_name string, is_top int, goods_brand_ids string) (int64, string) {
	o := orm.NewOrm()
	var goodsType = GoodsType{}

	goodsType.Parent_id = parent_id
	goodsType.Type_name = type_name
	goodsType.Is_top = is_top
	goodsType.Goods_brand_ids = goods_brand_ids

	_, err := o.Insert(&goodsType)
	if err != nil {
		return CODE1006, MSG1006
	} else {
		return CODE1000, MSG1000
	}
}

//修改商品类型
func UpdateGoodsTypeData(id int64, parent_id int64, type_name string, is_top int, goods_brand_ids string) (int64, string) {
	o := orm.NewOrm()
	var goodsType = GoodsType{}
	goodsType.Id = id

	//检查该类型是否存在
	if o.Read(&goodsType) == nil {
		//修改商品类型信息
		goodsType.Parent_id = parent_id
		goodsType.Type_name = type_name
		goodsType.Is_top = is_top
		goodsType.Goods_brand_ids = goods_brand_ids

		_, err := o.QueryTable("shop_goods_type").Filter("id", goodsType.Id).Update(orm.Params{
			"parent_id":       goodsType.Parent_id,
			"type_name":       goodsType.Type_name,
			"is_top":          goodsType.Is_top,
			"goods_brand_ids": goodsType.Goods_brand_ids,
		})

		if err != nil {
			return CODE1006, MSG1006
		} else {
			return CODE1000, MSG1000
		}
	} else {
		return CODE1008, MSG1008
	}
}

//新增商品类型SEO
func AddGoodsTypeSeoData(type_id int64, title string, keyword string, description string) (int64, string) {
	o := orm.NewOrm()

	//检查该分类的SEO 是否存在

	_, err := o.Raw("INSERT INTO shop_goods_type_seo (type_id,title,keyword,description) VALUES (?,?,?,?)", type_id, title, keyword, description).Exec()

	if err != nil {
		return CODE1006, MSG1006
	} else {
		return CODE1000, MSG1000
	}
}

//编辑商品类型SEO
func UpdateGoodsTypeSeoData(id int64, type_id int64, title string, keyword string, description string) (int64, string) {
	o := orm.NewOrm()
	var goodsTypeSeo = GoodsTypeSeo{}

	checkErr := o.Raw("SELECT * FROM shop_goods_type_seo WHERE id=?", id).QueryRow(&goodsTypeSeo)
	//检查该类型SEO是否存在
	if checkErr == nil {
		//修改商品类型SEO信息

		_, err := o.Raw("UPDATE shop_goods_type_seo SET type_id=?,title=?,keyword=?,description=? WHERE id=? ", type_id, title, keyword, description, id).Exec()

		if err != nil {
			return CODE1006, MSG1006
		} else {
			return CODE1000, MSG1000
		}
	} else {
		return CODE1008, MSG1008
	}
}

//商品分类（子级，父级）列表
func GetGoodsTypeList(pagenum int64, page int64, parent_id int64) (int64, string, interface{}, *PageBody) {
	o := orm.NewOrm()
	var goodsType []GoodsType
	var pageBody = PageBody{}
	qs := o.QueryTable("shop_goods_type").Filter("parent_id", parent_id)

	//计算总个数
	pageBody.Count, _ = qs.Count()
	//获取设置单页个数
	pageBody.PageSetNum = pagenum
	//获取当前页
	pageBody.Page = page

	//计算总页数
	pageBody.PageCount = math.Ceil((float64(pageBody.Count) / float64(pageBody.PageSetNum)))
	_, err := qs.Limit(pageBody.PageSetNum, pageBody.PageSetNum*(page-1)).All(&goodsType)

	if err != nil {
		return CODE1005, MSG1005, goodsType, &pageBody
	} else {
		return CODE1000, MSG1000, goodsType, &pageBody
	}
}
